<?php
namespace app\service;

use think\Db;
use Vectorface\Whip\Whip;

class UserDevice
{
    public static function InsertS($user_id, $device_id)
    {
        $hasRecord = Db::name('user_device_records')->where(['user_id'=>$user_id, 'device_id'=>$device_id])->find();
        $ip = (new Whip())->getValidIpAddress();
        $timenow = time();
        if ($hasRecord) {
            Db::name('user_device_records')->where(['id'=>$hasRecord['id']])->update([
                'ip_last'=>$ip,
                'login_num'=>Db::raw('login_num+1'),
                'update_time'=>$timenow
            ]);
        } else {
            Db::name('user_device_records')->insert([
                'user_id'=>$user_id,
                'device_id'=>$device_id,
                'ip_last'=>$ip,
                'login_num'=>1,
                'create_time'=>$timenow
            ]);
        }
    }
}
